define(function (require) {

    require('./refund-tpl');

    var Tpl = juicer.templates['refund.juicer.html'];


    var refund = {

        detailEntry: {},

        load: false,

        init: function (id) {
            var self = this;
            CRM.request({
                "type": 'GET',
                "url": CRM.getOuterUrl("/trade/order/refund/queryRefundReason"),
                "dataType": "json",
                contentType: "application/x-www-form-urlencoded",
                "success": function (response) {
                    if (response.status) {
                        CRM.request({
                            "type": 'GET',
                            "url": CRM.getOuterUrl("/trade/order/refund/queryBizOrderWithRefund"),
                            "dataType": "json",
                            contentType: "application/x-www-form-urlencoded",
                            "data": {
                                "bizOrderId": id
                            },
                            "success": function (req) {
                                if (req.status) {
                                    self.detailEntry = req.entry;
                                    self.detailEntry.refundReason = response.entry;
                                    self.registerJuicerFunc();
                                    self.build();
                                    self.bindEvent();
                                    self.initValidator();
                                } else {
                                    CRM.showDialog(req.message);
                                }
                            }
                        });
                    } else {
                        CRM.showDialog(response.message);
                    }
                }
            });

        },
        /**
         * 注册Juicer可执行的js方法
         */
        registerJuicerFunc: function () {
            var self = this;
            juicer.register('checkRefundReasonCode', self.checkRefundReasonCode);
        },
        bindEvent: function () {
            var self = this;
            $("#refund_btn").on('click', function () {
                self.createRefund();
            });
        },
        /**
         * 界面渲染
         */
        build: function () {
            CRM.showModal(Tpl({
                "data": this.detailEntry
            }), $('body'));
        },
        /**
         * 表单验证初始化
         */
        initValidator: function () {
            $('#refund_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    fee: {
                        message: '退款金额输入错误',
                        validators: {
                            notEmpty: {
                                message: '退款金额不能为空'
                            },
                            numeric: {
                                message: '请输入有效的数字'
                            },
                            callback: {
                                message: '超过最大可退款金额',
                                callback: function (value, validator) {
                                    if (Number(value) > Number($("#refund_fee_max").text())) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            });
        },
        ///**
        // * 查询最大可退款金额
        // */
        //queryMaxRefundFee: function () {
        //    var self = this;
        //    $("#refund_form").data('bootstrapValidator').resetForm();
        //    $("#refund_form").data('bootstrapValidator').validate();
        //    if ($("#refund_form").data('bootstrapValidator').isValid()) {
        //        var refundSubOrderMap = {};
        //        $("input[id^='items_']").each(function () {
        //            refundSubOrderMap[$(this).attr("data-temId")] = $(this).val();
        //        });
        //        CRM.request({
        //            "type": 'POST',
        //            "url": CRM.getOuterUrl("/trade/order/refund/queryMaxRefundFee"),
        //            "dataType": "json",
        //            data: JSON.stringify({
        //                "bizOrderId": self.detailEntry.bizOrderDO.bizOrderId,
        //                "refundSubOrderMap": refundSubOrderMap
        //            }),
        //            "success": function (response) {
        //                if (response.status) {
        //                    $("#refund_fee_max").text(response.entry / 100);
        //                    $("#refund_fee_div").show();
        //                    $("#refund_btn").text("确认退款");
        //                    $("#refund_btn").unbind();
        //                    $("input[id^='items_']").attr("disabled", "disabled");
        //                    $("#refund_btn").on('click', function () {
        //                        self.createRefund();
        //                    });
        //                } else {
        //                    CRM.showDialog(response.message);
        //                }
        //            }
        //        });
        //    }
        //},
        /**
         * 确认退款
         */
        createRefund: function () {
            var self = this;
            $("#refund_form").data('bootstrapValidator').resetForm();
            $("#refund_form").data('bootstrapValidator').validate();
            if ($("#refund_form").data('bootstrapValidator').isValid()) {
                var refundSubOrderMap = {};
                $("input[id^='items_']").each(function () {
                    refundSubOrderMap[$(this).attr("data-temId")] = $(this).val();
                });
                var refundSubOrderFeeMap = {};
                $("input[id^='fee_']").each(function () {
                    refundSubOrderFeeMap[$(this).attr("data-temId")] = $(this).val()*100;
                });
                var psfRefundFee;
                psfRefundFee = $("#psf_fee").val()*100;
                CRM.request({
                    "type": 'POST',
                    "url": CRM.getOuterUrl("/trade/order/refund/createRefund"),
                    "dataType": "json",
                    data: JSON.stringify({
                        "bizOrderId": self.detailEntry.bizOrderDO.bizOrderId,
                        "refundReasonCode": $("#refundReasonCode").val(),
                        "refundSubOrderMap": refundSubOrderMap,
                        "refundSubOrderFeeMap": refundSubOrderFeeMap,
                        psfRefundFee:psfRefundFee
                    }),
                    "success": function (response) {
                        if (response.status) {
                            CRM.showDialog(response.message);
                            $('#refund-detail-modal').modal('hide');
                            window.doSearch();
                        } else {
                            CRM.showDialog(response.message);
                        }
                    }
                });
            }
        },
        /**
         * 退款原因
         * @param refundReasonCode
         */
        checkRefundReasonCode: function (refundReasonCode) {
            return refund.detailEntry.refundReason[refundReasonCode];
        }

    };


    return refund;

});